package com.qlmy.cn.ui.widget

import android.content.Context
import android.content.res.TypedArray
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import android.util.AttributeSet
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.view.ViewCompat
import com.qlmy.cn.commom.dp
import com.qlmy.cn.commom.toColor
import com.qlmy.cn.ui.listener.OnValidationResultListener
import com.qlmy.cn.utils.VariableViewUtils
import com.wbs.shop.nuby.R

class CommonButton : AppCompatTextView, View.OnClickListener, OnValidationResultListener {
    companion object {
        const val CB_NORMAL = 1 // 直接使用主色调非空心
        const val CB_STROKE_TEXT = 2 // 字体色色非空心
        const val CB_RED = 3 // 红色字体色色非空心
    }

    private var mOnClickListener: OnClickListener? = null
    private var defaultEnable = true// 是否活跃
    private var mVariableViewUtils: VariableViewUtils? = null
    private var normalDrawable: Drawable? = null
    private var enableFalseDrawable: Drawable? = null
    private var mCBStyle = CB_NORMAL
    private var radius = 5.dp.toFloat()
    private var stroke = 1.dp
    private var textSize = 15.dp

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        setOnClickListener(this)
        init(attrs)
    }

    fun init(attrs: AttributeSet?) {
        attrs?.let {
            val typedArray: TypedArray = context.obtainStyledAttributes(attrs, R.styleable.CommonButton)
            mCBStyle = typedArray.getInt(R.styleable.CommonButton_cb_style, CB_NORMAL)
            textSize = typedArray.getDimensionPixelSize(R.styleable.CommonButton_but_text_size, textSize)
            radius = typedArray.getDimension(R.styleable.CommonButton_radius, radius)
            typedArray.recycle()
        }
        changeStyle()
        enableFalseDrawable = GradientDrawable().apply { setColor(R.color.text_hint_color.toColor());cornerRadius = radius }
        setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize.toFloat())
        gravity = Gravity.CENTER
    }

    fun changeStyle(cbStyle: Int = mCBStyle) {
        when (cbStyle) {
            CB_NORMAL -> {
                normalDrawable = GradientDrawable().apply { setColor(R.color.colorPrimary.toColor());cornerRadius = radius }
                setTextColor(R.color.white.toColor())
            }
            CB_RED -> {
                normalDrawable = GradientDrawable().apply { setColor(R.color.button_red.toColor());cornerRadius = radius }
                setTextColor(R.color.white.toColor())
            }
            CB_STROKE_TEXT -> {
                normalDrawable = GradientDrawable().apply {
                    cornerRadius = radius
                    setStroke(stroke, R.color.text_color.toColor())
                }
                setTextColor(R.color.text_color.toColor())
            }
            /* CB_STROKE_NORMAL -> {
                 normalDrawable = GradientDrawable().apply {
                     cornerRadius = radius
                     setStroke(stroke, R.color.main_color.toColor())
                 }
                 setTextColor(R.color.main_color.toColor())
             }
             CB_WHITE -> {
                 normalDrawable = GradientDrawable().apply { setColor(R.color.white.toColor());cornerRadius = radius }
                 setTextColor(R.color.text_hint_color_2.toColor())
             }
             CB_BG -> {
                 normalDrawable = GradientDrawable().apply { setColor(R.color.bg.toColor());cornerRadius = radius }
                 setTextColor(R.color.text_hint_color_2.toColor())
             }
             CB_MAIN_LIGHT -> {
                 normalDrawable = GradientDrawable().apply { setColor(R.color.main_color_light.toColor());cornerRadius = radius }
                 setTextColor(R.color.main_color.toColor())
             }
             CB_STROKE_TEXT -> {
                 normalDrawable = GradientDrawable().apply {
                     cornerRadius = radius
                     setStroke(stroke, R.color.text_color.toColor())
                 }
                 setTextColor(R.color.text_color.toColor())
             }
             CB_BLUE -> {
                 normalDrawable = GradientDrawable().apply { setColor(R.color.blue.toColor());cornerRadius = radius }
                 setTextColor(R.color.white.toColor())
             }*/
        }
        ViewCompat.setBackground(this, if (defaultEnable) normalDrawable else enableFalseDrawable)
    }

    override fun setOnClickListener(l: OnClickListener?) {
        if (this == l)
            super.setOnClickListener(this)
        else
            mOnClickListener = l
    }

    fun setVariableButtonUtils(variableViewUtils: VariableViewUtils) {
        mVariableViewUtils = variableViewUtils
    }

    override fun onClick(v: View?) {
        if (mOnClickListener != null && defaultEnable) {
            //if (BLUtils.notRepeatClick()) {
            if (mVariableViewUtils == null) {
                mOnClickListener?.onClick(v)
            } else if (mVariableViewUtils!!.clickValidator()) {
                mOnClickListener?.onClick(v)
            }
            //}
        }
    }

    override fun enable(enable: Boolean) {
        if (enable && !defaultEnable) ViewCompat.setBackground(this, normalDrawable)
        if (!enable && defaultEnable) ViewCompat.setBackground(this, enableFalseDrawable)
        defaultEnable = enable
    }
}